
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * 刪除源碼
 */
public class DeleteSrc {

    private final Path JAVA_MODULES;
    private final Path JAVA_EXTERNAL_CLASSES;

    private final PathMatcher javaMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.{java}");


    public DeleteSrc(String FIELD_NC_HOME) {
        JAVA_MODULES = Paths.get(FIELD_NC_HOME, "modules");
        JAVA_EXTERNAL_CLASSES = Paths.get(FIELD_NC_HOME, "external/classes");
    }

    public static void run(String home) throws IOException {
        new DeleteSrc(home).run();
    }

    public void run() throws IOException {
        delJava();
    }


    private void delJava() throws IOException {
        _doDel(JAVA_EXTERNAL_CLASSES);
        _doDel(JAVA_MODULES);

        System.out.println(">>> delete **/*.java success");
    }


    private void _doDel(Path javaPath) throws IOException {
        Files.walkFileTree(javaPath, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path source, BasicFileAttributes attrs) throws IOException {
                if (javaMatcher.matches(source)) {
                    System.out.println("delete " + source);
                    source.toFile().delete();
                }

                return FileVisitResult.CONTINUE;
            }


            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    }

    public static void main(String[] args) throws IOException {
        new DeleteSrc("F:/cloud/mokohome").run();
    }
}
